/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.dao.hibernate;

import com.minierp.dao.MakeRequireDao;
import com.minierp.dao.hibernate.BaseDaoImpl;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class MakeRequireDaoImpl extends BaseDaoImpl implements MakeRequireDao {
	public List getList(String searchKey) {
		String sql = "from com.minierp.model.MakeRequire c";
		if (searchKey != null) {
			searchKey = searchKey.trim();
		}

		if (searchKey != null && !searchKey.equals("")) {
			sql = sql
					+ " where ((c.requireContent like :searchKey) or (c.code like :searchKey))";
		}

		sql = sql + " order by c.sequenceCode";
		Session sess = null;

		List arg5;
		try {
			sess = this.getSession();
			Query q = sess.createQuery(sql);
			if (searchKey != null && !searchKey.equals("")) {
				q.setString("searchKey", "%" + searchKey + "%");
			}

			arg5 = q.list();
		} finally {
			if (sess != null) {
				this.releaseSession(sess);
			}

		}

		return arg5;
	}

	public int delete(final long id) {
		Object obj = this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						String hql = "delete com.minierp.model.MakeRequire where id = :id";
						return new Integer(session.createQuery(hql)
								.setLong("id", id).executeUpdate());
					}
				});
		return ((Integer) obj).intValue();
	}

	public int getMaxSequenceCode() {
		String sql = "select max(c.sequenceCode) from com.minierp.model.MakeRequire c";
		List list = this.getHibernateTemplate().find(sql);
		return !list.isEmpty() && list.get(0) != null ? Integer.parseInt(list
				.get(0).toString()) : 0;
	}
}